package com.sinosoft.modules.code;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import com.sinosoft.common.action.BaseAction;
import com.sinosoft.common.bean.BaseCondition;
import com.sinosoft.common.util.Constans;
import com.sinosoft.common.web.filter.BaseFilter;
import com.sinosoft.common.web.filter.UserLoginFilter;
import com.sinosoft.domain.code.CodeDbConfig;
import com.sinosoft.plugin.code.NutzDaoUtil;

/**
 * @author yangqunwei
 * @mail yqw8912@163.com
 * @time  2015-05-20 14:17:07 
 * 数据库配置-t_code_db_config Action
 */
@IocBean
@At("/code/CodeDbConfig")
@Filters({@By(type = UserLoginFilter.class), @By(type = BaseFilter.class)})
public class CodeDbConfigAction extends BaseAction {
	//查询列表
	@At("")
	@Ok("jsp:jsp.code.listCodeDbConfig")
	public void list(@Param("..")BaseCondition baseCondition,@Param("..")CodeDbConfig codeDbConfig,HttpServletRequest req) {
		dao.create(CodeDbConfig.class, false);
		daoUtil.setPowerLevel(Constans.P_USER);
		List<CodeDbConfig> codeDbConfigList = daoUtil.queryList(codeDbConfig,baseCondition);
		req.setAttribute("codeDbConfigList", codeDbConfigList);//查询结果返回页面 
		req.setAttribute("baseCondition", baseCondition);//总记录数返回页面
		req.setAttribute("codeDbConfig", codeDbConfig);//返回查询条件鞭面页面跳转后查询条件丢失
	}
	//准备新增
	@At
	@Ok("jsp:jsp.code.editCodeDbConfig")
	public void toadd(HttpServletRequest req) {
		req.setAttribute("codeDbConfig", new CodeDbConfig());
		req.setAttribute("actionType", "add");
	}	
	/**
	 * 新增数据
	 * @param req
	 */
	@At
	public void add(HttpServletRequest req,HttpServletResponse response,@Param("..")CodeDbConfig codeDbConfig) {
		try {
			daoUtil.add(codeDbConfig);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxDoneError(response,e.getMessage());		
		}	
		ajaxDoneSuccess(response,successMessage,true);
	}	
	//准备更新
	@At
	@Ok("jsp:jsp.code.editCodeDbConfig")
	public void toedit(HttpServletRequest req,@Param("id") String id) {
		req.setAttribute("codeDbConfig",daoUtil.detailByPK(CodeDbConfig.class,id));
		req.setAttribute("actionType", "edit");
	}
	/**
	 * 新增数据
	 * @param req
	 */
	@At	
	public void edit(HttpServletRequest req,HttpServletResponse response,@Param("..")CodeDbConfig codeDbConfig) {
		try {
			daoUtil.update(codeDbConfig);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxDoneError(response,e.getMessage());		
		}	
		ajaxDoneSuccess(response,successMessage,true);
	}
	/**
	 * 测试数据源
	 * @param req
	 */
	@At	
	public void test(HttpServletRequest req,HttpServletResponse response,@Param("id") String id) {
		try {
			CodeDbConfig config = daoUtil.detailByPK(CodeDbConfig.class,id);
			Dao dao = new NutzDaoUtil().getNutzDao(config);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxDoneError(response,e);		
		}	
		ajaxDoneSuccess(response,"数据源连接成功!");
	}
	//批量删除
	@At	
	public  void deletes(HttpServletRequest req,HttpServletResponse response,@Param("ids") String[] idArray) {
		try {
			for(int i=0;i<idArray.length;i++){
				daoUtil.deleteByPK(CodeDbConfig.class,idArray[i]);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajaxDoneError(response,e.getMessage());		
		}		
		ajaxDoneSuccess(response,successMessage);
	}
	//批量删除
	@At	
	public  void delete(HttpServletRequest req,HttpServletResponse response,@Param("id") String id) {
		try {
				daoUtil.deleteByPK(CodeDbConfig.class,id);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxDoneError(response,e.getMessage());		
		}		
		ajaxDoneSuccess(response,successMessage);
	}	
	@At	
	@Ok("jsp:jsp.code.editCodeDbConfig")
	public void view(HttpServletRequest req,@Param("id") String id) {
		req.setAttribute("codeDbConfig",daoUtil.detailByPK(CodeDbConfig.class,id));
		req.setAttribute("actionType", "view");
	}
}